/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.is.cef.command.listener;

import cef.command.Command;
import cef.command.listener.CommandOutputEvent;
import cef.command.listener.CommandOutputListenerException;
import cef.command.listener.CommandPrintWriter;
import coalescent.is.cef.command.RunSamplers_Multi_Cmd;
import commons.util.ArrayUtil;
import commons.util.PrintUtil;

import java.math.BigDecimal;

import static commons.util.FormatUtil.format;

/**
 * @since
 * @version
 * @author Susanta Tewari
 * @history Created on 2/4/13.
 */
public class RunSamplers_Multi_PW extends CommandPrintWriter {

    @Override
    public void receivedEventImpl(final CommandOutputEvent cmdEvent)
            throws CommandOutputListenerException {

        final RunSamplers_Multi_Cmd cmd = (RunSamplers_Multi_Cmd) cmdEvent.getSource();
        final BigDecimal[] params       = cmd.getEvent().getParams();
        final Object[][] results        = new Object[params.length + 1][4];

        results[0] = new String[] { "Parameter", "Mean", "SE", "ESS" };    // header

        for (final String samplerName : cmd.getSamplerNames()) {    // MVE

            getPrintWriter().println("Sampler: " + samplerName);

            final BigDecimal[] isMean = cmd.getISMean(samplerName);
            final int indexOfMax      = ArrayUtil.indexOfMax(isMean);

            getPrintWriter().println("Maximum Value Estimator: " + format(params[indexOfMax]));
            getPrintWriter().println("\n");
        }

        for (final String samplerName : cmd.getSamplerNames()) {    // table values

            getPrintWriter().println("Sampler: " + samplerName);

            final BigDecimal[] isMean = cmd.getISMean(samplerName);
            final BigDecimal[] isSE   = cmd.getIS_SE(samplerName);
            final BigDecimal[] isESS  = cmd.getIS_ESS(samplerName);

            for (int i = 0; i < params.length; i++) {    // data

                results[i + 1] = new String[] { format(params[i]), format(isMean[i]),
                                                format(isSE[i]), format(isESS[i]) };
            }

            getPrintWriter().println(PrintUtil.print2D(results, 2, 1, null));
            getPrintWriter().flush();
        }
    }

    @Override
    public RunSamplers_Multi_PW newInstance() {
        return new RunSamplers_Multi_PW();
    }

    @Override
    public Class<? extends Command<?>> getTargetCommandClass() {
        return RunSamplers_Multi_Cmd.class;
    }
}
